package com.tqz.rc.spring.cloud.registry;

import com.tqz.rc.spring.cloud.TqzRcProperties;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.ApplicationContext;

import java.net.URI;
import java.util.Map;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2021/9/2 14:33
 */
public class TqzRcRegistration implements Registration, ServiceInstance {

    private TqzRcProperties tqzRcProperties;

    private ApplicationContext applicationContext;

    public TqzRcRegistration(TqzRcProperties tqzRcProperties, ApplicationContext applicationContext) {
        this.tqzRcProperties = tqzRcProperties;
        this.applicationContext = applicationContext;
    }

    @Override
    public String getServiceId() {
        return tqzRcProperties.getServiceName();
    }

    @Override
    public String getHost() {
        return tqzRcProperties.getIp();
    }

    public void setHost(String host) {
        tqzRcProperties.setIp(host);
    }

    @Override
    public int getPort() {
        return tqzRcProperties.getPort();
    }

    public void setPort(int port) {
        this.tqzRcProperties.setPort(port);
    }

    @Override
    public boolean isSecure() {
        return false;
    }

    @Override
    public URI getUri() {
        return DefaultServiceInstance.getUri(this);
    }

    @Override
    public Map<String, String> getMetadata() {
        return null;
    }

    public TqzRcProperties getTqzRcProperties() {
        return tqzRcProperties;
    }

    public void setTqzRcProperties(TqzRcProperties tqzRcProperties) {
        this.tqzRcProperties = tqzRcProperties;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}
